﻿token = "d6e9b132a9fa0b8c8b59e564d156d23c285f03ec0e88715b888e5ad740b00f9e";
enterprise_id = "17ac0597546ed761f9c6f30c48beb45e60e52711";
function getSignture(Type, URI, data, token, reqEntity) {
    // type:请求方式  uri:vl后面的值  data:请求参数  token:token  reqEntity:消息实体

    var entity = reqEntity;
    if (entity.length > 100)
        entity = entity.substring(0, 100);
    console.log("entity:" + entity);

    var entitySHA = CryptoJS.SHA256(entity);
    console.log("entitySHA:" + entitySHA);

    var entityBase64 = CryptoJS.enc.Base64.stringify(entitySHA);
    console.log("entityBase64:" + entityBase64);

    var signBefore = Type + "\n" + URI + "\n" + data + "\n" + entityBase64;
    console.log("signBefore:" + signBefore);

    var signatureSHA = CryptoJS.HmacSHA256(signBefore, token);// token 对待编码字符串加密
    console.log("signatureSHA:" + signatureSHA);

    var signatureBase64 = CryptoJS.enc.Base64.stringify(signatureSHA); // base64位
    console.log("signatureBase64:" + signatureBase64);

    var signAfter = encodeURIComponent(signatureBase64);     // URI 编码
    console.log("signatureBase64:" + signAfter);

    return signAfter;

    ///*
    //待签名的字符串 =
    //    请求方法 + '\n' +
    //    请求URI + '\n' +
    //    格式化后的请求参数 + '\n' +
    //    Base64(Sha256Hash(请求实体))
    //    */
    //var arr = [], arr1 = [], Data = {}, Data2 = {};
    //arr.push(Type);
    //arr.push(URI);
    //// 将请求参数 data 赋值给 Data
    //for (var key in data) {
    //    Data[key] = data[key];
    //}
    ///** 请求参数进行URI编码 并且按照编码后的key值进行排序  ***/
    ////进行编码 
    //for (var key in Data) {
    //    arr1.push(encodeURIComponent(key));
    //    //console.log(encodeURIComponent(key), encodeURIComponent(Data[key]));
    //    Data2[encodeURIComponent(key)] = encodeURIComponent(Data[key]);
    //}
    //arr1.sort();//对key进行排序
    
    //for (var i = 0; i < arr1.length; i++) {
    //    arr1[i] = arr1[i] + "=" + Data2[arr1[i]]
    //} //[key]==>[key=value]

    //arr1 = arr1.join("&");// [key=value,key2=value2]  ==> key=value&key2=value2
    //arr.push(arr1);// [Type,URI,key=value&key2=value2]
    //if (!reqEntity || Type.toLowerCase() == "get" || Type.toLowerCase() =="delete") {
    //    var hash = CryptoJS.SHA256("");
    //} else { 
    //    var hash = CryptoJS.SHA256(reqEntity.slice(0, 100));
        
    //}
    //console.log(hash);
    //var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
    //arr.push(hashInBase64);
    //var str = arr.join("\n"); //生成了待编码字符串

    //var signature2 = CryptoJS.HmacSHA256(str, token);// token 对待编码字符串加密
    //signature2 = CryptoJS.enc.Base64.stringify(signature2); // base64位
    //signature2 = encodeURIComponent(signature2);     // URI 编码
    //console.log(signature2);
    //return signature2;
}

function stringToBytes(str) {
    var ch, st, re = [];
    for (var i = 0; i < str.length; i++) {
        ch = str.charCodeAt(i);  // get char  
        st = [];                 // set up "stack"  

        do {
            st.push(ch & 0xFF);  // push byte to stack  
            ch = ch >> 8;          // shift value down by 1 byte  
        }
        while (ch);
        // add stack contents to result  
        // done because chars have "wrong" endianness  
        re = re.concat(st.reverse());
    }
    // return an array of bytes  
    return re.slice(0,100).join("");
} 
/*
var str = '000000';
var bytes = stringTobytes(str);
*/

function isArrayFn(value) {
    if (typeof Array.isArray === "function") {
        return Array.isArray(value);
    } else {
        return Object.prototype.toString.call(value) === "[object Array]";
    }
}
//alert(isArrayFn(arr));// true
//alert(isArrayFn(arr2));// true


/****
 
var data = {
    enterprise_id: enterprise_id
}
data.signature = getSignture("GET", "buffet/nemos", data, token);
**/

/* test 测试  根据 官网例子
var str=G_str = "GET\n"
        + "create_meeting\n"
        + "end_time=1450923006431&"
        + "enterprise_id=" + enterprise_id + "&"
        + "max_participant=50&"
        + "meeting_name=test&"
        + "require_password=true&"
        + "start_time=1450923006431\n"
        + "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=";
console.log(str);

var signature = CryptoJS.HmacSHA256(str, token);
console.log(signature);
signature = CryptoJS.enc.Base64.stringify(signature);
signature = encodeURIComponent(signature);
console.log(signature, signature == "mhPZmgyeOt6Z0sAaaCLeCrLQF7haQhVKa7M2M%2FN8D8o%3D");
*/